package com.ben.multiple.fdfs;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadFileStream;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import java.io.IOException;
import java.io.OutputStream;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * FastDFSClient 工具类
 */
@Component
@Slf4j
public class FastDfsClient {
    private final FastFileStorageClient storageClient;
    private final FdfsWebServer fdfsWebServer;

    public FastDfsClient(@Qualifier("defaultFastFileStorageClient") FastFileStorageClient storageClient,
                         FdfsWebServer fdfsWebServer) {
        this.storageClient = storageClient;
        this.fdfsWebServer = fdfsWebServer;
    }

    /**
     * 上传一般文件
     *
     * @param file 多个文件
     */
    public String uploadFile(MultipartFile file) {
        try {
            StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
                    this.getFileExtension(file), null);
            return storePath.getFullPath();
        } catch (IOException e) {
            log.error("FastDfsClient uploadFile error : ", e);
            throw new RuntimeException("FastDfsClient uploadFile error");
        }
    }

    /**
     * 上传图片并且生成缩略图；支持的图片格式包括"JPG", "JPEG", "PNG", "GIF", "BMP", "WBMP"
     */
    public String uploadImageAndCrtThumbImage(MultipartFile file) {
        try {
            StorePath storePath = storageClient.uploadImageAndCrtThumbImage(
                    file.getInputStream(), file.getSize(), this.getFileExtension(file), null);
            return storePath.getFullPath();
        } catch (IOException e) {
            log.error("FastDfsClient uploadImageAndCrtThumbImage error : ", e);
            throw new RuntimeException("FastDfsClient uploadImageAndCrtThumbImage error");
        }
    }

    public void deleteFile(String filePath) {
        if (StringUtils.hasText(filePath)) {
            storageClient.deleteFile(filePath);
        }
    }

    public void downloadFile(String filePath, OutputStream outputStream) {
        StorePath storePath = StorePath.parseFromUrl(filePath);
        DownloadFileStream downloadFileStream = new DownloadFileStream(outputStream);
        storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), downloadFileStream);
    }

    private String getFileExtension(MultipartFile file) {
        String fileName = StringUtils.hasText(file.getOriginalFilename())
                ? file.getOriginalFilename() : file.getName();
        return FilenameUtils.getExtension(fileName);
    }

    public String getFullUrl(String filePath) {
        return fdfsWebServer.getWebServerUrl() + filePath;
    }
}
